package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.exception.BusinessException;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author liuyp
 * @since 2025/7/25
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentMapper studentMapper;
    @Override
    public PageBean<Student> page(String name, Integer degree, Integer clazzId, Integer page, Integer pageSize) {
        //开启分页
        PageHelper.startPage(page, pageSize);

        //查询列表
        List<Student> list = studentMapper.pageList(name, degree, clazzId);

        //封装结果
        Page<Student> p = (Page<Student>) list;

        return new PageBean<>((int) p.getTotal(), list);
    }

    @Override
    public void add(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount((short) 0);
        student.setViolationScore((short) 0);
        studentMapper.insert(student);
    }

    @Override
    public Student getById(Integer id) {
        return studentMapper.selectById(id);
    }

    @Override
    public void updateById(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateById(student);
    }

    @Override
    public void deleteByIds(Integer[] ids) {
        if (ids == null || ids.length==0) {
            throw new BusinessException("请选择要删除的学生");
        }
        studentMapper.deleteByIds(ids);
    }

    @Override
    public void violation(Integer studentId, Integer score) {
        studentMapper.violation(studentId, score);
    }
}
